package com.snj.visangedu.listeningmax;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.snj.visangedu.R;
import com.snj.visangedu.lib.Global;


public class FolderAdapter extends BaseAdapter {
	
	String tag = "FolderAdapter";
	
	Context context;
	int layout;
	
	
	ArrayList<FolderItem> mFolderList;
	
	LayoutInflater mInflater = null;
	
	public FolderAdapter(Context _context, int _layout, ArrayList<FolderItem> _newslist) {
		// TODO Auto-generated constructor stub
		
		this.context = _context;
		this.layout = _layout;
		this.mFolderList = _newslist;
		
	}
	
	public void Insert(ArrayList<FolderItem> mList)
	{
		mFolderList.addAll(mList);
	}
	
	public void SetList(ArrayList<FolderItem> mList)
	{
		mFolderList = mList;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mFolderList.size();
	}

	public FolderItem getItem(int position) {
		// TODO Auto-generated method stub
		return mFolderList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
	
		final FolderHolder holder;// = null;
		
		if(convertView == null)
		{
			mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(layout, parent, false);
			
			holder = new FolderHolder();
			
			holder.txt_foldername = (TextView)convertView.findViewById(R.id.txt_foldername);
			holder.txt_folderpath = (TextView)convertView.findViewById(R.id.txt_folderpath);
			holder.img_controlfolder = (ImageView)convertView.findViewById(R.id.img_controlfolder);
			holder.img_changefoldername = (ImageView)convertView.findViewById(R.id.img_change_foldername);
			holder.lily_medias = (LinearLayout)convertView.findViewById(R.id.lily_medias);
			convertView.setTag(holder);
			
			
		}
		else
		{
			holder = (FolderHolder)convertView.getTag();
		}
		
		holder.txt_foldername.setText(mFolderList.get(position).GetFolderName().length() > 18 ? mFolderList.get(position).GetFolderName().substring(0, 18) : mFolderList.get(position).GetFolderName());
		holder.txt_folderpath.setText(mFolderList.get(position).GetFolderPath().length() > 20 ? mFolderList.get(position).GetFolderPath().substring(0, 20) : mFolderList.get(position).GetFolderPath());
		
		holder.img_controlfolder.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(holder.lily_medias.getVisibility() == View.VISIBLE)
				{
					holder.lily_medias.setVisibility(View.GONE);
				}
				else
				{
					holder.lily_medias.setVisibility(View.VISIBLE);
				}
			}
		});
		
		ArrayList<MediaItem> mListMedia = mFolderList.get(position).GetMediaList();
		
		//holder.lily_medias.removeAllViews();
		if(holder.lily_medias.getChildCount() == 0)
		{
			for(int i=0; i<mListMedia.size(); i++)
			{
				RelativeLayout rlly_media = new RelativeLayout(context);
				LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, Global.dpToPx(context, 64));
				rlly_media.setLayoutParams(param);
				
				RelativeLayout.LayoutParams childparam = new RelativeLayout.LayoutParams(Global.dpToPx(context, 58), Global.dpToPx(context, 50));
				childparam.addRule(RelativeLayout.CENTER_VERTICAL);
				childparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				childparam.rightMargin = Global.dpToPx(context, 10);
				
				ImageView img_change_filename = new ImageView(context);
				img_change_filename.setScaleType(ScaleType.FIT_END);
				img_change_filename.setImageResource(R.drawable.listeningmax_change_filename);
				img_change_filename.setLayoutParams(childparam);
				
				rlly_media.addView(img_change_filename);
				
				childparam = new RelativeLayout.LayoutParams(Global.dpToPx(context, 58), Global.dpToPx(context, 50));
				childparam.addRule(RelativeLayout.CENTER_VERTICAL);
				childparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				childparam.rightMargin = Global.dpToPx(context, 78);
				
				ImageView img_add_play = new ImageView(context);
				img_add_play.setScaleType(ScaleType.FIT_END);
				img_add_play.setImageResource(R.drawable.listeningmax_add_play);
				img_add_play.setLayoutParams(childparam);
				img_add_play.setId(4580);
				
				
				childparam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				childparam.addRule(RelativeLayout.CENTER_VERTICAL);
				childparam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				childparam.addRule(RelativeLayout.LEFT_OF, img_add_play.getId());
				childparam.leftMargin = Global.dpToPx(context, 10);
				
				
				TextView txt_medianame = new TextView(context);
				txt_medianame.setTextColor(Color.parseColor("#FF666666"));
				txt_medianame.setText(mListMedia.get(i).GetMediaName());
				txt_medianame.setTextSize(14);
				txt_medianame.setLayoutParams(childparam);
				
				rlly_media.addView(txt_medianame);
				
				rlly_media.addView(img_add_play);
				
				holder.lily_medias.addView(rlly_media);
			}
			holder.lily_medias.setVisibility(View.GONE);
		}
		
		//
		
		
		
		return convertView;
	}
	
	
}
